package com.js.nickcar.module.addcar.uploadphoto;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;

import com.js.nickcar.R;
import com.js.nickcar.base.BaseFragment;
import com.js.nickcar.entity.local.UploadCarPhotoBean;
import com.js.nickcar.entity.local.UploadPhotoBean;
import com.js.nickcar.module.addcar.valuation.ValuationEntryFragment;
import com.js.nickcar.utils.PickUtil;


import butterknife.Bind;
import me.nereo.multi_image_selector.MultiImageSelectorActivity;

/**
 * Created by LZY on 2017/10/7.
 */

public class UploadCarPhotoFragment extends BaseFragment<UploadCarPhotoPresenter> implements UploadCarPhotoView, UploadCarPhotoAdapter.OnUploadCarPhotoListener {
    @Bind(R.id.recyclerView)
    RecyclerView mRecyclerView;
    private UploadCarPhotoAdapter mAdapter;
    public UploadPhotoBean mUploadPhotoBean;
    private static final String EXTRA_CAR_ID = "car_id";

    @Override
    public int getLayoutId() {
        return R.layout.fragment_car_upload_photo;
    }

    @Override
    protected void initBundle(Bundle bundle) {
        super.initBundle(bundle);
        mPresenter.setCarId(bundle.getString(EXTRA_CAR_ID));
    }

    @Override
    protected UploadCarPhotoPresenter createPresenter() {
        return new UploadCarPhotoPresenter();
    }

    public static UploadCarPhotoFragment getInstance(String carId) {
        if (TextUtils.isEmpty(carId)) throw new RuntimeException("请传入车辆id");
        UploadCarPhotoFragment uploadCarPhotoFragment = new UploadCarPhotoFragment();
        Bundle bundle = new Bundle();
        bundle.putString(EXTRA_CAR_ID, carId);
        uploadCarPhotoFragment.setArguments(bundle);
        return uploadCarPhotoFragment;
    }

    @Override
    protected String getTitle() {
        return getString(R.string.upload_photo);
    }

    @Override
    protected void initData() {
        mAdapter = new UploadCarPhotoAdapter(getContext());
        mAdapter.setOnUploadCarPhotoListener(this);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecyclerView.setAdapter(mAdapter);
    }


    @Override
    public void onAddPhoto(UploadPhotoBean uploadPhotoBean) {
        mUploadPhotoBean = uploadPhotoBean;
        PickUtil.pickImage(this, false);
    }

    @Override
    public void onNext() {
        mPresenter.commitCarImagesInfo(mAdapter.getItems());
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != Activity.RESULT_OK) {
            return;
        }

        switch (requestCode) {
            case PickUtil.REQUEST_CODE_IMAGE:
                String imageUrl = data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT).get(0);
                uploadImage(imageUrl);
                break;
            case PickUtil.REQUEST_STORAGE_READ_ACCESS_PERMISSION:
                PickUtil.pickImage(this, false);//请求权限成功后再次打开图片选择
                break;
        }

        if (requestCode == getTargetRequestCode()) {
            getTargetFragment().onActivityResult(requestCode, resultCode, data);
            finish();
        }
    }

    private void uploadImage(String imageUrl) {
        mUploadPhotoBean.setLocalPath(imageUrl);
        mPresenter.uploadImage(mUploadPhotoBean);
    }

    @Override
    public void uploadImageSuccess(UploadPhotoBean uploadPhotoBean) {
        for (int i = 0; i < mAdapter.getItems().size(); i++) {
            UploadCarPhotoBean item = mAdapter.getItem(i);
            if (item.getType().equals(uploadPhotoBean.getType())) {
                item.getUploadPhotos().add(uploadPhotoBean);
                mAdapter.notifyItemChanged(i);
            }
        }
    }

    /**
     * 提交车辆图片信息成功
     *
     * @param carId
     */
    @Override
    public void commitCarImagesSuccess(String carId) {
        ValuationEntryFragment entryFragment = ValuationEntryFragment.getInstance(carId);
        entryFragment.setTargetFragment(this,getTargetRequestCode());
        addFragment(entryFragment);
    }

}
